package com.example.advancecomponents.adaptors;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;

import com.example.advancecomponents.vo.Star;
import com.example.advancecomponents.fragment.StarDetailFragment;
import java.util.List;

public class StarDetailFragmentPagerAdapter extends FragmentPagerAdapter {
    private final List<Star> stars;

    public StarDetailFragmentPagerAdapter(@NonNull FragmentManager fm, List<Star> stars) {
        // 有多个 Fragment 时
        // 只有当前正在显示的Fragment 可以是 Resume 状态
        super(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
        this.stars = stars;
    }

    @NonNull
    @Override
    public Fragment getItem(int position) {
        // 适配器在这里创建 Fragment
        //
        return StarDetailFragment.newInstance(stars.get(position),position);
    }

    @Override
    public int getCount() {
        return stars.size();
    }

    @Nullable
    @Override
    public CharSequence getPageTitle(int position) {
        return stars.get(position).getName();
    }
}
